package com.example.controller;

import com.example.pojo.Change;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.impl.UserServiceImpl;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.DuplicateFormatFlagsException;

@Slf4j
@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private HttpServletRequest request;

    //注册
    /*@PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info("kas");
        System.out.println(user.toString());
        if(user.getAccount()==null || user.getAccount()==""){
            return Result.error("请输入账号");
        }
        if(user.getPasswords()==null || user.getPasswords()==""){
            return Result.error("请输入密码");
        }
        if(user.getUserType()==null){
            return Result.error("请选择用户类型");
        }
        userService.register(user);
        return Result.success();
    }*/


    //修改密码
    @PutMapping("/changemm")
    public Result changeMm(@RequestBody Change change){
        if(change.getPasswords()==null || change.getPasswords()=="" || change.getPasswordsOne()==null || change.getPasswordsOne()=="" || change.getPasswordsTwo()==null || change.getPasswordsTwo()==""){
            return Result.error("请输入新旧密码");
        }
        if(change.getPasswordsOne().equals(change.getPasswordsTwo())==false){
            return Result.error("两次密码不相同");
        }
        String passwordss=userService.getPasswords(request);
        if(change.getPasswords().equals(passwordss)==false){
            return Result.error("旧密码错误");
        }
        userService.changeMm(change,request);
        return Result.success();
    }

    //个人信息显示接口
    @GetMapping
    public Result pim(){
        User user=userService.getPim(request);
        return Result.success(user);
    }

    //个人信息修改
    @PutMapping
    public Result setPim(@RequestBody User user){
        userService.setPim(user,request);
        return Result.success();
    }

}
